<?php

namespace App\Services;

use App\Models\ShouqianbaSetting as Setting;
class ShouqianbaSetting extends Base
{
    public function getList($shopBns)
    {
        $res = Setting::whereIn('shop_bn', $shopBns)->get()->toArray();
        if (empty($res)) return [];

        foreach ($res as $k => $v) {
            $res[$k]['expire_time'] = $v['expire_time'] / 60;
        }
        return $res;
    }

    public function setting($params)
    {
        $settingInfo = Setting::where('shop_bn', '=', $params['shop_bn'])->first();
        $data = [
            'shop_bn' => $params['shop_bn'],
            'brand_code' => isset($params['brand_code']) ? $params['brand_code'] : $settingInfo->brand_code,
            'app_id' => isset($params['app_id']) ? $params['app_id'] : $settingInfo->app_id,
            'expire_time' => isset($params['expire_time']) ? $params['expire_time'] * 60 : $settingInfo->expire_time,
            'private_key' => isset($params['private_key']) ? $params['private_key'] : $settingInfo->private_key,
            'public_key' => isset($params['public_key']) ? $params['public_key'] : $settingInfo->public_key,
        ];
        if (empty($settingInfo)) {
            $res = Setting::insert($data);
            if (empty($res))
                return ['error' => true, 'message' => '配置失败'];
        } else {
            $res = Setting::where('shop_bn', '=', $data['shop_bn'])->update($data);
            if ($res === false)
                return ['error' => true, 'message' => '配置更新失败'];
        }
        return ['error' => false];
    }
}